/*
 * @FileName: app_comm.h
 * @Author: GreyQiu qiushaogui@aikosolar.com
 * @Date: 2023-08-21 16:44:51
 * @LastEditors: GreyQiu qiushaogui@aikosolar.com
 * @LastEditTime: 2023-09-26 10:48:42
 * @Description: 
 * 
 * Copyright (c) 2023, All Rights Reserved. 
 */

#ifndef __BMS_CAN_H__
#define __BMS_CAN_H__

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>

// 电池保护 数据来自0X359
typedef union {
    uint16_t word;
    struct {
        uint16_t cmdOff : 1;
        uint16_t ovp : 1;
        uint16_t uvp : 1;
        uint16_t otpCell : 1;
        uint16_t utpCell : 1;
        uint16_t otpEnv : 1;
        uint16_t utpEnv : 1;
        uint16_t ocpDischg : 1;
        uint16_t ocpChg : 1;
        uint16_t pwrOtp : 1;
        uint16_t cellLV : 1;
        uint16_t chgHV : 1;
        uint16_t reserve : 4;
    } bits;
} bms_protect_t;

// 电池告警 数据来自0X359
typedef union {
    uint16_t word;
    struct {
        uint16_t reserve1 : 1;
        uint16_t ovw : 1;
        uint16_t uvw : 1;
        uint16_t otwCell : 1;
        uint16_t utwCell : 1;
        uint16_t otwEnv : 1;
        uint16_t utwEnv : 1;
        uint16_t ocwDischg : 1;
        uint16_t ocwChg : 1;
        uint16_t pwrOtw : 1;
        uint16_t reserve2 : 1;
        uint16_t commErr : 1;
        uint16_t cellErr : 1;
        uint16_t tmpSensorErr : 1;
        uint16_t lowSoc : 1;
        uint16_t fullSoc : 1;
    } bits;
} bms_alarm_t;

// 电池常规信息
typedef struct {
    // 来自0X359
    uint16_t cap;
    // 来自0X351
    uint16_t maxVol;
    uint16_t currChg;
    uint16_t currDisChg;
    uint16_t minVol;
    //来自0X355
    uint16_t SOC;
    uint16_t SOH;
    uint16_t cellMaxVol;
    uint16_t cellMinVol;
    // 来自0X356
    uint16_t voltage;
    int16_t current;
    uint16_t cellMaxTmp;
    uint16_t cellMinTmp;
} bms_data_t;

typedef struct
{
    uint16_t cmd;    // 命令码
    void (*handler)(uint16_t cmd, uint16_t * buf, uint16_t len);
} bsm_cmd_t;

#define BSM_CMD_NUM 4

void bms_handle(uint16_t cmd, uint16_t * data, uint32_t len);

#endif // __APP_WIFI_H__
